package com.ruoyi.utils;

import com.ruoyi.mapper.LowCarbonActivityMapper;
import com.ruoyi.mapper.TanpuhuiUtilsMapper;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.domain.UserInfo;
import com.ruoyi.system.domain.vo.GoodsSystemVo;
import com.ruoyi.system.service.IGoodsService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;

@Component
public class TanpuhuiUtils {

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private IGoodsService goodsServiceImpl;

    @Autowired
    private LowCarbonActivityMapper lowCarbonActivityMapper;

    @Autowired
    private TanpuhuiUtilsMapper tanpuhuiUtilsMapper;

    /**
     * 调用字典，获取商品类型标签
     * @param goodsTypeId 商品类型ID
     * @return 商品类型标签
     */
    public String getGoodsTypeLabel(Long goodsTypeId) {
        return sysDictDataService.selectDictLabel("goods_type", String.valueOf(goodsTypeId));
    }

    /**
     * 调用字典，获取所有相关的商品类型Id
     * @param goodsTypeId
     * @return 所有相关的商品类型Id列表
     */
    public List<Long> getAllRelevantTypeIds(Long goodsTypeId) {
//        System.out.println("(getAllRelevantTypeIds)"+"goodsTypeId: " + goodsTypeId);
        String TypeRemark = sysDictDataService.selectRemark("goods_type", String.valueOf(goodsTypeId));
//        System.out.println("(getAllRelevantTypeIds)"+"TypeRemark: " + TypeRemark);
        return sysDictDataService.getAllRelevantTypeIds("goods_type", TypeRemark);
    }

    /**
     * 获取所有符合类型Id列表的商品
     */
    public List<GoodsSystemVo> selectAllTypeGoodsList(List<Long> relevantTypeIdList, Long pageNum, Long pageSize, String sortWay, boolean feeShipping){
//        System.out.println("(selectAllTypeGoodsList)"+"relevantTypeIdList: " + relevantTypeIdList);
        return goodsServiceImpl.selectAllTypeGoodsList(relevantTypeIdList, pageNum, pageSize, sortWay, feeShipping);
    }

    /**
     * 获取所有该类型的商品
     * @param goodsTypeId 商品类型ID
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param sortWay 排序方式
     * @return 该类型的商品列表
     */
    public List<GoodsSystemVo> selectTypeGoodsList(Long goodsTypeId, Long pageNum, Long pageSize, String sortWay, boolean feeShipping){
        return goodsServiceImpl.selectTypeGoodsList(goodsTypeId, pageNum, pageSize, sortWay, feeShipping);
    }

    /**
     * 通过价格对商品列表进行降序排序
     * @param goodsList 商品列表
     * @return 降序排序后的商品列表
     */
    public List<Goods> sortPriceDesc(List<Goods> goodsList){
        // 价格降序排序
        goodsList.sort(Comparator.comparing(Goods::getGoodsPrice).reversed());
//        System.out.println("(sortPriceDesc)"+goodsList);
        return goodsList;
    }

    /**
     * 通过价格对商品列表进行升序排序
     * @param goodsList 商品列表
     * @return 升序排序后的商品列表
     */
    public List<Goods> sortPriceAsc(List<Goods> goodsList){
        // 价格升序排序
        goodsList.sort(Comparator.comparing(Goods::getGoodsPrice));
        return goodsList;
    }

    /**
     * 通过销量对商品列表进行降序排序
     * @param goodsList 商品列表
     * @return 降序排序后的商品列表
     */
    public List<Goods> sortSaleCount(List<Goods> goodsList) {
        // 销量降序排序
        goodsList.sort(Comparator.comparing(Goods::getSaleCount).reversed());
        return goodsList;
    }

    /**
     * 综合排序商品列表 (！暂未实现)
     * @param goodsList 商品列表
     * @return 综合排序后的商品列表
     */
    public List<Goods> sortComprehensive(List<Goods> goodsList) {
        return goodsList;
    }

    /**
     * 获取商品规格的序号
     * @param goodsId 商品ID
     * @param standerSon 商品子规格
     * @return 商品规格的序号
     */
     public int getStanderIndex(String standerSon, Long goodsId) {
         int index = 0;
         Goods goods = goodsServiceImpl.selectGoodsByGoodsId(goodsId);
         String stander = goods.getStander();
         String[] split = stander.split("/");
//         System.out.println("(getStanderIndex)"+"standerSon: " + standerSon);
//         System.out.println("(getStanderIndex)"+"stander: " + stander);
//         System.out.println("(getStanderIndex)"+"split: " + Arrays.toString(split));
         for (int i = 0; i < split.length; i++) {
             if (split[i].equals(standerSon)) {
                 index = i;
                 return index;
             }
         }
         return -1;
     }

    /**
     * 获取活动已结束，并且没有结算活动奖励的活动
     */
    public List<Activity> getNoSettleActivityList() {
        return lowCarbonActivityMapper.getNoSettleActivityList();
    }

    /**
     * 活动奖励结算
     * @param activityId 活动ID
     */
    public void settleActivityReward(Long activityId) {
        lowCarbonActivityMapper.settleActivityReward(activityId);
    }

    /**
     * 更新用户的减碳时间
     */
    public void updateUserReductionTime(Long userId) {
        int co2ReduceTime = 0;
        LocalDate date = LocalDate.now().minusDays(1);
        while (isUserReduction(userId, date)){
            co2ReduceTime++;
            date = date.minusDays(1);
        }
//        System.out.println("(updateUserReductionTime)"+"userId: " + userId + ", co2ReduceTime: " + co2ReduceTime);
        tanpuhuiUtilsMapper.updateUserReductionTime(userId, co2ReduceTime);
    }

    /**
     * 判断某天用户是否减碳
     */
    public boolean isUserReduction(Long userId, LocalDate date) {
        return tanpuhuiUtilsMapper.isHaveCo2Reduce(userId, date);
    }
}
